let express = require("express");
let app = express();
app.use(express.json({ limit: "100mb" }));
app.use(express.urlencoded({ limit: "100mb", extended: true }));
// app.set('port', port);

//解决跨域
app.use((req, res, next) => {
  // 设置是否运行客户端设置 withCredentials
  // 即在不同域名下发出的请求也可以携带 cookie
  res.header("Access-Control-Allow-Credentials", true);
  // 第二个参数表示允许跨域的域名，* 代表所有域名
  res.header("Access-Control-Allow-Origin", "http://localhost"); //为什么设置了特定域名还不起效？？
  res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS"); // 允许的 http 请求的方法
  // 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type, Authorization, Content-Length, X-Requested-With"
  );
  if (req.method == "OPTIONS") {
    res.sendStatus(200);
  } else {
    next();
  }
});

app.use(express.json()); //用于解析前台发过来的json

// 1.upload_byfront 图片由前台获取token并上传
let upload_byfront_router = require("./routes/upload_byfront");
app.use("/upload_byfront", upload_byfront_router);

var http = require("http");
//套接字模块
//创建HTTP server
var server = http.createServer(app);
//监听3001端口
app.listen(3001, () => {
  console.log("服务器启动……");
});
// 导出
module.exports = app;
